package cn.jsu.oj.dispatchServer.satellite.handler;

import cn.jsu.oj.dispatchServer.satellite.annotation.HandlerType;
import cn.jsu.oj.dispatchServer.satellite.config.ResourceConfig;
import cn.jsu.oj.dispatchServer.satellite.factory.Factory;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.*;
import cn.jsu.oj.dispatchServer.satellite.util.FileUtil;
import cn.jsu.oj.dispatchServer.satellite.util.LogPointUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 日志处理器
 *
 * @author shan
 * @date 2022/07/05
 */
@Slf4j
@Component("logHandler")
@HandlerType(typeNo = 8)
public class LogHandler implements Handler {

    @Autowired
    Factory handlerFactory;

    /**
     * 日志目录
     */
    private static final String LOG_PATH = ResourceConfig.getConfigString("client-request-log");

    /**
     * 处理
     *
     * @param packNet net包
     */
    @Override
    public void handle(PackNet<?> packNet) throws FileNotFoundException {
        RequestNet requestNet = (RequestNet) packNet.getMessage();
        String uuid = requestNet.getUuid();
        byte[] bytes = FileUtil.fileReader(LOG_PATH + requestNet.getLogName());
        ((Handler) handlerFactory.getProduct((short) 4))
                .handle(new PackNet<>(packNet.getChannel(),
                        new ResponseNet(uuid, true, (short) 8, JSON.toJSONString(bytes))));
    }
}
